package com.dc.management.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.dc.connectionfactory.ConnectionFactory;
import com.dc.management.pojo.TbDept;
import com.dc.management.pojo.TbModule;
import com.dc.management.pojo.TbUser;

/**
 * ��װ���к�User�йص���ݿ����
 * */
public class UserDAO {
	
	//��ѯ�����û�
	public List findAllUser(){
		List users = new ArrayList();
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try{
			con = ConnectionFactory.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append(" select USER_ID,DEPT_ID,WORK_NO,LOGIN_NAME, ");
			sql.append(" sex,password,MOBILE_PHONE,WORK_PHONE,EMAIL, ");
			sql.append(" NAME,POST,FIRSTMODULE,REMARK,CREATEMENU, ");
			sql.append(" ISUSER,valid_flag ");
			sql.append(" from tb_user ");
			pstm = con.prepareStatement(sql.toString());
			rs = pstm.executeQuery();
			while(rs.next()){
				TbUser user = new TbUser();
				user.setUser_id(rs.getInt("user_id"));
				user.setUser_name(rs.getString("user_name"));
				user.setSex(rs.getString("sex"));
				users.add(user);//���û���ӵ�����
			}
			return users;
		}catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
		finally{
			ConnectionFactory.close(con, pstm, rs);
		}
		
	}
	
	public TbUser isExiest(TbUser user){
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try{
			con = ConnectionFactory.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append(" SELECT USER_ID,USER_NAME,PASSWORD,SEX,DEPT_ID FROM TB_USER WHERE USER_NAME = ? AND PASSWORD = ? ");
			pstm = con.prepareStatement(sql.toString());
			pstm.setString(1, user.getUser_name());
			pstm.setString(2, user.getPassword());
			rs = pstm.executeQuery();
			//����ҵ�,���û���Ϣ��װ���������淵��,û�ҵ�����null
			if(rs.next()){
				user.setUser_id(rs.getInt("user_id"));
				user.setSex(rs.getString("sex"));
				return user;
			}
			return null;
		}catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
		finally{
			ConnectionFactory.close(con, pstm, rs);
		}
	}
	
	//����û�Id�����û����ܷ��ʵ�ģ��(Ȩ��)  /configure/management/module/module_add.jsp
	public List<TbModule> findModuleInfoByUserId(int user_id){
		List<TbModule> modules = new ArrayList<TbModule>();
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try{
			con = ConnectionFactory.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append(" SELECT module_id,upper_module_id,module_name,url ");
			sql.append(" FROM tb_module  ");
			sql.append(" WHERE module_id IN(   ");
			sql.append(" SELECT tbm_module FROM tb_role_module   ");
			sql.append(" WHERE tbm_role IN(  ");
			sql.append(" SELECT tur_role FROM tb_user_role  ");
			sql.append(" WHERE tur_user = ? )) ");
			//sql.append(" select user_id from tb_user where user_id=?))) ");
			pstm = con.prepareStatement(sql.toString());
			pstm.setInt(1, user_id);
			rs = pstm.executeQuery();
			while(rs.next()){
				TbModule module = new TbModule();
				module.setModule_id(rs.getInt("module_id"));
				module.setUpper_module_id(rs.getInt("upper_module_id"));
				module.setModule_name(rs.getString("module_name"));
				module.setUrl(rs.getString("url"));
				modules.add(module);
			}
			return modules;
		}catch(Exception ex){
			System.out.println(ex.getMessage());
			ex.printStackTrace();
			return null;
		}
		finally{
			ConnectionFactory.close(con, pstm, rs);
		}
	}
	
	public static void main(String[] args){
		UserDAO dao = new UserDAO();
		TbUser user = new TbUser();
		user.setUser_id(1);
		//user.setLogin_name("caocao");
		user.setPassword("1111");
		user = dao.isExiest(user);
		List<TbModule> list = dao.findModuleInfoByUserId(user.getUser_id());
		for(TbModule module : list){
			System.out.println("id -- " + module.getModule_id());
			System.out.println("name -- " + module.getModule_name());
			System.out.println("url -- " + module.getUrl());
			System.out.println("upper_module_id -- " + module.getUpper_module_id());
		}
	}

}
